package com.aidong.util;

/**
 * Created by john on 16-11-2.
 */
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.aidong.model.wills.ContractInfo;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Bj004Util {
    protected static Logger log = Logger.getLogger(Bj004Util.class);
    protected static String callUrl = "http://111.204.57.2:8080/ChiaTai/salestrans.asmx";
    protected static String userName = "3A021A01";
    protected static String userPwd = "3A021A01";
    protected static String mallId = "SHC591";
    protected static String storeCode = "SHC5913A021A01";
    protected static String itemCode = "SHC5913A021A011";

    public Bj004Util() {
    }

    public static boolean postSales(ContractInfo data, String id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmss");
        DecimalFormat nf = new DecimalFormat("0.00");
        String sdate = sdf.format(data.getSaleDate());
        String stime = sdf2.format(data.getSaleDate());
        double money = data.getAmount1().doubleValue() + data.getAmount2().doubleValue() + data.getAmount3().doubleValue();
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n  <soap12:Body>\n    <postsalescreate xmlns=\"http://tempurl.org\">\n      <astr_request>\n        <header>\n          <licensekey></licensekey>\n          <username>" + userName + "</username>\n" + "          <password>" + userPwd + "</password>\n" + "          <lang></lang>\n" + "          <messagetype>SALESDATA</messagetype>\n" + "          <messageid>332</messageid>\n" + "          <version>V332M</version>\n" + "        </header>\n" + "        <salestotal>\n" + "          <localstorecode></localstorecode>\n" + "          <reservedocno></reservedocno>\n" + "          <txdate_yyyymmdd>" + sdate + "</txdate_yyyymmdd>\n" + "          <txtime_hhmmss>" + stime + "</txtime_hhmmss>\n" + "          <mallid>" + mallId + "</mallid>\n" + "          <storecode>" + storeCode + "</storecode>\n" + "          <tillid>01</tillid>\n" + "          <salestype>SA</salestype>\n" + "          <txdocno>" + id + "</txdocno>\n" + "          <orgtxdate_yyyymmdd></orgtxdate_yyyymmdd>\n" + "          <orgstorecode></orgstorecode>\n" + "          <orgtillid></orgtillid>\n" + "          <orgtxdocno></orgtxdocno>\n" + "          <mallitemcode>" + itemCode + "</mallitemcode>\n" + "          <cashier>666666</cashier>\n" + "          <vipcode></vipcode>\n" + "          <salesman></salesman>\n" + "          <demographiccode></demographiccode>\n" + "          <demographicdata></demographicdata>\n" + "          <netqty>1</netqty>\n" + "          <originalamount>" + nf.format(money) + "</originalamount>\n" + "          <sellingamount>" + nf.format(money) + "</sellingamount>\n" + "          <couponnumber></couponnumber>\n" + "          <coupongroup></coupongroup>\n" + "          <coupontype></coupontype>\n" + "          <totaldiscount>\n" + "            <salesdiscount xsi:nil=\"true\" />\n" + "            <salesdiscount xsi:nil=\"true\" />\n" + "          </totaldiscount>\n" + "          <netamount>" + nf.format(money) + "</netamount>\n" + "          <paidamount>" + nf.format(money) + "</paidamount>\n" + "          <changeamount>0</changeamount>\n" + "          <priceincludetax></priceincludetax>\n" + "          <shoptaxgroup></shoptaxgroup>\n" + "          <extendparam></extendparam>\n" + "          <invoicetitle></invoicetitle>\n" + "          <invoicecontent></invoicecontent>\n" + "          <issueby>666666</issueby>\n" + "          <issuedate_yyyymmdd>" + sdf.format(new Date()) + "</issuedate_yyyymmdd>\n" + "          <issuetime_hhmmss>" + sdf2.format(new Date()) + "</issuetime_hhmmss>\n" + "          <ecorderno></ecorderno>\n" + "          <buyerremark></buyerremark>\n" + "          <orderremark></orderremark>\n" + "          <status></status>\n" + "          <ttpossalesdocno></ttpossalesdocno>\n" + "        </salestotal>\n" + "        <salesitems>\n" + "          <salesitem>\n" + "            <iscounteritemcode>1</iscounteritemcode>\n" + "            <lineno>1</lineno>\n" + "            <storecode>" + storeCode + "</storecode>\n" + "            <mallitemcode>" + itemCode + "</mallitemcode>\n" + "            <counteritemcode>" + itemCode + "</counteritemcode>\n" + "            <itemcode>" + itemCode + "</itemcode>\n" + "            <plucode>" + itemCode + "</plucode>\n" + "            <colorcode></colorcode>\n" + "            <sizecode></sizecode>\n" + "            <qty>1</qty>\n" + "            <exstk2sales>1</exstk2sales>\n" + "            <originalprice>" + nf.format(money) + "</originalprice>\n" + "            <sellingprice>" + nf.format(money) + "</sellingprice>\n" + "            <pricemode></pricemode>\n" + "            <priceapprove></priceapprove>\n" + "            <couponnumber></couponnumber>\n" + "            <coupongroup></coupongroup>\n" + "            <coupontype></coupontype>\n" + "            <itemdiscount xsi:nil=\"true\" />\n" + "            <vipdiscountpercent>0</vipdiscountpercent>\n" + "            <vipdiscountless>0</vipdiscountless>\n" + "            <promotion xsi:nil=\"true\" />\n" + "            <totaldiscountless1>0</totaldiscountless1>\n" + "            <totaldiscountless2>0</totaldiscountless2>\n" + "            <totaldiscountless>0</totaldiscountless>\n" + "            <tax xsi:nil=\"true\" />\n" + "            <netamount>" + nf.format(money) + "</netamount>\n" + "            <bonusearn>0</bonusearn>\n" + "            <salesitemremark></salesitemremark>\n" + "            <refundreasoncode></refundreasoncode>\n" + "            <extendparam></extendparam>\n" + "          </salesitem>\n" + "        </salesitems>\n" + "        <salestenders>\n";
        int ln = 1;
        if(data.getAmount1().doubleValue() != 0.0D) {
            content = content + "          <salestender>\n            <lineno>" + ln++ + "</lineno>\n" + "            <tendercode>CH</tendercode>\n" + "            <payamount>" + nf.format(data.getAmount1()) + "</payamount>\n" + "            <baseamount>" + nf.format(data.getAmount1()) + "</baseamount>\n" + "            <excessamount>0</excessamount>\n" + "            <extendparam></extendparam>\n" + "            <remark></remark>\n" + "          </salestender>\n";
        }

        if(data.getAmount2().doubleValue() != 0.0D) {
            content = content + "          <salestender>\n            <lineno>" + ln++ + "</lineno>\n" + "            <tendercode>CI</tendercode>\n" + "            <payamount>" + nf.format(data.getAmount2()) + "</payamount>\n" + "            <baseamount>" + nf.format(data.getAmount2()) + "</baseamount>\n" + "            <excessamount>0</excessamount>\n" + "            <extendparam></extendparam>\n" + "            <remark></remark>\n" + "          </salestender>\n";
        }

        if(data.getAmount3().doubleValue() != 0.0D) {
            content = content + "          <salestender>\n            <lineno>" + ln++ + "</lineno>\n" + "            <tendercode>OT</tendercode>\n" + "            <payamount>" + nf.format(data.getAmount3()) + "</payamount>\n" + "            <baseamount>" + nf.format(data.getAmount3()) + "</baseamount>\n" + "            <excessamount>0</excessamount>\n" + "            <extendparam></extendparam>\n" + "            <remark></remark>\n" + "          </salestender>\n";
        }

        content = content + "        </salestenders>\n      </astr_request>\n    </postsalescreate>\n  </soap12:Body>\n</soap12:Envelope>";
        Document doc = call(content);
        if(doc == null) {
            return false;
        } else {
            Element root = doc.getRootElement();
            String code = root.element("Body").element("postsalescreateResponse").element("postsalescreateResult").element("header").elementText("responsecode");
            return "0".equals(code);
        }
    }

    public static boolean postSummary(Date date, String id, int quan, double money) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmss");
        DecimalFormat nf = new DecimalFormat("0.00");
        String sdate = sdf.format(date);
        String stime = sdf2.format(date);
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n  <soap12:Body>\n    <postdailysalessummary xmlns=\"http://tempurl.org\">\n      <astr_request>\n        <header>\n          <licensekey></licensekey>\n          <username>" + userName + "</username>\n" + "          <password>" + userPwd + "</password>\n" + "          <lang></lang>\n" + "          <messagetype>SALESDATA</messagetype>\n" + "          <messageid>332</messageid>\n" + "          <version>V332M</version>\n" + "        </header>\n" + "        <salessummary>\n" + "          <localstorecode>" + storeCode + "</localstorecode>\n" + "          <txdate_yyyymmdd>" + sdate + "</txdate_yyyymmdd>\n" + "          <txtime_hhmmss>" + stime + "</txtime_hhmmss>\n" + "          <mallid>" + mallId + "</mallid>\n" + "          <storecode>" + storeCode + "</storecode>\n" + "          <tillid>01</tillid>\n" + "          <txdocno>" + id + "</txdocno>\n" + "          <ttlsalesqty>" + quan + "</ttlsalesqty>\n" + "          <ttlrefundqty>0</ttlrefundqty>\n" + "          <ttlsalesamt>" + nf.format(money) + "</ttlsalesamt>\n" + "          <ttlrefundamt>0</ttlrefundamt>\n" + "          <ttldoccount>0</ttldoccount>\n" + "          <cashier>666666</cashier>\n" + "          <issuedate_yyyymmdd>" + sdf.format(new Date()) + "</issuedate_yyyymmdd>\n" + "        </salessummary>\n" + "      </astr_request>\n" + "    </postdailysalessummary>\n" + "  </soap12:Body>\n" + "</soap12:Envelope>";
        Document doc = call(content);
        if(doc == null) {
            return false;
        } else {
            Element root = doc.getRootElement();
            String code = root.element("Body").element("postdailysalessummaryResponse").element("postdailysalessummaryResult").element("header").elementText("responsecode");
            return "0".equals(code);
        }
    }

    protected static Document call(String data) {
        Document doc = null;

        try {
            byte[] e = data.getBytes("utf-8");
            URL url1 = new URL(callUrl);
            HttpURLConnection http = (HttpURLConnection)url1.openConnection();
            http.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            http.setRequestProperty("Content-Length", String.valueOf(e.length));
            http.setConnectTimeout(30000);
            http.setReadTimeout('\uea60');
            http.setRequestMethod("POST");
            http.setDoOutput(true);
            OutputStream os = http.getOutputStream();
            os.write(e);
            os.flush();
            os.close();
            if(http.getResponseCode() == 200) {
                doc = getResponse(http.getInputStream());
            } else {
                getResponse(http.getErrorStream());
            }

            http.disconnect();
        } catch (Exception var6) {
            log.error("callUrl: " + callUrl, var6);
        }

        return doc;
    }

    protected static Document getResponse(InputStream is) throws Exception {
        char[] cc = new char[1024];
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
        StringBuilder sb = new StringBuilder();

        int len;
        while((len = br.read(cc)) != -1) {
            sb.append(cc, 0, len);
        }

        br.close();
        Document doc = DocumentHelper.parseText(sb.toString());
        return doc;
    }
}
